package com.wangwei.basic;

import edu.princeton.cs.algs4.StdOut;

public class FixedCapacityStack<Item> {
    private Item[] a;
    /**
     * 注意，此处是数量，不是下标。
     */
    private int N;
    public FixedCapacityStack(int cap){
        a=(Item[]) new Object[cap];
    }

    public boolean isEmpty(){
        return N == 0 ;
    }

    public int size(){
        return N;
    }

    public void push(Item item){
        a[N++] = item ;
    }

    public Item pop(){
        return a[--N];//此处size转换为index
    }

    public static void test(String exp){
        String[] arr = exp.split(" ");
        FixedCapacityStack<String> s =new FixedCapacityStack<>(100);
        for(String item : arr){
            if(!item.equals("-")){
                s.push(item);
            }else if(!s.isEmpty()) StdOut.println(s.pop()+"");
        }
        StdOut.println("("+s.size()+" left on stack)" );
    }

    public static void main(String[] args) {
        String exp="to be or not to - be - - that - - - is";
        test(exp);
    }
}
